<?php
/**
 * Created by : qicaitong.net
 * User: 内蒙古启讯科技信息有限公司
 * Tel: 15648133366（微信）
 */

namespace app\api\model;


use app\admin\controller\SystemAdmin;
use think\Model;
use think\Request;

class AdminModel extends Model
{
    protected $pk = 'id';
    protected $table = 'bk_admin';
    protected $resultSetType = '';

    /**
     * 读取系统管理员及代理商
     * @get
     */
    public function get_system_admin() {
        $id = Request::instance()->get('id/d');
        if($id == 0 || $id == null || $id == '')
        {
            return json(AdminModel::all());
        }
        return json(AdminModel::all($id));
    }

    /**
     * 添加系统管理员及代理商
     * @post
     */
    public function post_system_admin() {
        $data = Request::instance()->except(['id'],'post');
        $data['addtime'] = time();
        $data['password'] = md5($data['password']);
        if(AdminModel::create($data)) {
            return json(['code' => 1, 'msg'=>'添加数据成功']);
        }
        return json(['code' => -1, 'msg'=>'添加数据失败']);
    }

    /**
     * 删除系统管理员及代理商
     * @delete
     */
    public function delete_system_admin() {
        $id = trim(Request::instance()->delete('id'),',');
        $id_array = array_map('intval', explode(",",$id));
        if(in_array(1, $id_array)) return json(['code' => -1, 'msg'=>'系统管理员不能删除']);
        if(AdminModel::destroy($id_array)) {
            return json(['code' => 1, 'msg'=>'删除数据成功']);
        }
        return json(['code' => -1, 'msg'=>'删除数据失败']);
    }

    /**
     * 修改系统管理员及代理商
     * @put
     */
    public function put_system_admin() {
        $id = Request::instance()->put("id/d");
        $data = Request::instance()->except(['id'],'put');
        $data['password'] = md5($data['password']);
        $result = AdminModel::where('id',$id)->update($data);
        return json(['msg'=>$result]);
    }
}